﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GovernmentOfficeProject.LevelBusinessLogic;
using GovernmentOfficeProject.LevelDataAccessModel;
using System.Collections;
using GovernmentOfficeProject.SysTools;

namespace GovernmentOfficeProject.LevelViewUI.admin
{
    public partial class UserManagerRole : System.Web.UI.Page
    {
        decimal dclValueParameter = 0;
        BLL_USER userBLL = new BLL_USER();
        BLL_RoleManager roleBLL = new BLL_RoleManager();
        Entity_USER_User userEntity = null;
        List<Entity_USER_Role> userRoleList = null;
        List<Entity_USER_Role> allRoleList = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            //权限判断
            if (SystemContext.CurrentUserID == 0 || SystemContext.IsAdmin == 0)
            {
                Response.Redirect("/LevelViewUI/admin/Login.htm?FromUrl=" + HttpUtility.UrlEncode(Request.Url.AbsoluteUri));
            }
            else
            {
                SystemContext.CurrentUserID = SystemContext.CurrentUserID;
                SystemContext.IsAdmin = 1;
            }
            if (Request.QueryString["val"] != null)
            {
                dclValueParameter = Convert.ToDecimal(Request.QueryString["val"].Trim());
                Entity_USER_User userOBJ = new Entity_USER_User();
                userOBJ.UserID = dclValueParameter;
                userEntity = userBLL.getUserByUser(userOBJ);    //获取当前用户.
            }
            else
            {
                dclValueParameter = 0;
            }
            //需判断是修改用户的角色(>0)还是用户新增加角色关联(==0).            
            if (!Page.IsPostBack)
            {
                if (dclValueParameter > 0)
                {
                    userRoleList = roleBLL.getRoleListByUser(userEntity);       //获取当前用户对应的用户组(角色)实体列表.
                    allRoleList = roleBLL.getRoleList();                        //获取所有用户组(角色).
                    GridViewRoleSettingValues();
                }
                else if (dclValueParameter.Equals(0))
                {
                    allRoleList = roleBLL.getRoleList();
                    GridViewRole.DataSource = allRoleList;
                    GridViewRole.DataBind();
                }
            }
        }

        /// <summary>
        /// 给GridViewRole赋值并判断用户组(角色)的选中状态.
        /// </summary>
        public void GridViewRoleSettingValues()
        {
            //以Map的形式保存用户所属的用户组(角色)，方便查找
            Dictionary<decimal, Entity_USER_Role> userRoleMap = new Dictionary<decimal, Entity_USER_Role>();
            foreach (Entity_USER_Role userRole in userRoleList)
            {
                userRoleMap.Add(userRole.RoleID, userRole);
            }
            foreach (Entity_USER_Role allRole in allRoleList)
            {
                if (userRoleMap.ContainsKey(allRole.RoleID))
                {
                    allRole.Selected = true;
                }
                else
                {
                    allRole.Selected = false;
                }
            }
            GridViewRole.DataSource = allRoleList;
            GridViewRole.DataBind();
        }

        /// <summary>
        /// 选中GridViewRole中每行的chackbox,触发添加删除用户对应用户组(角色)的操作.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ckbRoleSelect_CheckedChanged(object sender, EventArgs e)
        {
            List<Entity_USER_Role> roleList = null;

            if (((CheckBox)sender).Checked) //选中.
            {
                roleList = new List<Entity_USER_Role>();
                userBLL.deleteUserRoleByUserID(userEntity.UserID);  //←先删除对应用户组(角色),再重新添加↓.
                foreach (GridViewRow row in GridViewRole.Rows)
                {
                    if (((CheckBox)row.FindControl("ckbRoleSelect")).Checked)
                    {
                        Entity_USER_Role roleEntity = new Entity_USER_Role();
                        roleEntity.RoleID = Convert.ToDecimal(GridViewRole.DataKeys[row.RowIndex].Value);
                        roleList.Add(roleEntity);
                    }
                }
                bool blAddState = userBLL.addUserAndRolelist(userEntity, roleList);
                if (blAddState == true)
                {
                    //GridViewDeptSettingValues();
                    this.lblOperationMessage.Text = "提示: 设置用户组成功!";
                }
            }
            else
            {
                foreach (GridViewRow row in GridViewRole.Rows)
                {
                    if (!((CheckBox)row.FindControl("ckbRoleSelect")).Checked)
                    {
                        decimal roleID = Convert.ToDecimal(GridViewRole.DataKeys[row.RowIndex].Value);  //获取角色ID.

                        //判断role在Privilege是否有权限数据,如果有则提示不能删除.
                        List<Entity_SYS_Privilege> privilegeList = new BLL_Privilege().getPrivilegeListByRoleID(roleID);
                        List<Entity_USER_Relation> relationList = new BLL_UserRelation().getRelationListALL();
                        bool blIsExist = false;
                        foreach (Entity_SYS_Privilege privilege in privilegeList)
                        {
                            foreach (Entity_USER_Relation relation in relationList)
                            {
                                if (privilege.PriID == relation.UrControlledPrivilegeID)
                                {
                                    blIsExist = true;
                                    break;
                                }
                            }
                        }
                        if (blIsExist)
                        {
                            ((CheckBox)row.FindControl("ckbRoleSelect")).Checked = true;
                            this.lblOperationMessage.Text = "提示: 取消失败-用户组具有权限关联数据.";
                            //操作提示:
                            //Response.Write("<script LANGUAGE='javascript'>alert('XXXXXXXXX');</script>");
                        }
                        else
                        {
                            if (userBLL.deleteUserRoleByUserIDAndRoleID(userEntity.UserID, roleID))
                            {
                                this.lblOperationMessage.Text = "提示: 取消用户组成功!";
                            }

                        }
                    }
                }
            }
        }
    }
}


#region 过去的代码

//decimal dclValueParameter = 0;
//protected void Page_Load(object sender, EventArgs e)
//{
//    dclValueParameter = Convert.ToDecimal(Request.QueryString["val"]);

//}


///// <summary>
///// 为用户组GridViewRole赋值.
///// </summary>
//public void GridViewRoleSettingValues()
//{
//    //为用户组(角色)赋值:
//    BLL_USER userBLL = new BLL_USER();
//    Entity_USER_User user = userBLL.getUserByUserID(dclValueParameter);
//    List<Entity_USER_Role> userRoleList = userBLL.getRoleByUser(user);
//    List<Entity_USER_Role> allRoleList = new BLL_RoleManager().getRoleList();
//    ArrayList arrListRoleName = new ArrayList();
//    Dictionary<decimal, Entity_USER_Role> userRoleMap = new Dictionary<decimal, Entity_USER_Role>();
//    foreach (Entity_USER_Role userRole in userRoleList)
//    {
//        userRoleMap.Add(userRole.RoleID, userRole);
//    }
//    foreach (Entity_USER_Role allRole in allRoleList)
//    {
//        if (userRoleMap.ContainsKey(allRole.RoleID))
//        {
//            allRole.Selected = true;
//            arrListRoleName.Add(allRole.RoleName);
//        }
//        else
//        {
//            allRole.Selected = false;
//        }
//    }
//    GridViewRole.DataSource = allRoleList;
//    GridViewRole.DataBind();
//}


////弹出的用户组(角色)窗口--确定按钮操作
//protected void linkBtnRoleConfirm_Click(object sender, EventArgs e)
//{
//   // lblOperationState.Text = "成功保存用户所属用户组.";
//}

////弹出的用户组(角色)窗口--取消按钮操作
//protected void linkBtnRoleCancel_Click(object sender, EventArgs e)
//{
//    //lblOperationState.Text = "已取消用户所属用户组操作.";
//}
#endregion
